package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.annotation.RonghuanetPermission;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.query.EmployeeQuery;
import cn.ronghuanet.org.service.IEmployeeService;
import cn.ronghuanet.base.utils.AjaxResult;
import cn.ronghuanet.base.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的相关文档",description = "部门相关的CRUD功能")
//@CrossOrigin
@RonghuanetPermission(name = "员工管理")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /*
        添加和修改写一个接口
        RestFul请求风格:充分利用请求方式替换路径的里面动词
        http的请求方式:GET,
                    HEAD,
                    POST,
                    PUT,
                    PATCH,
                    DELETE,
                    OPTIONS,
                    TRACE;
        使用上面的请求方式替换路径里面的动词
         @RequestMapping(method = RequestMethod.PUT)  当前这个接口只允许使用PUT来请求

     */
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    @RonghuanetPermission(name = "员工的添加或修改")
    public AjaxResult saveOrUpdate(@RequestBody
                                   @ApiParam(value ="id为空为新增数据,id不为空则修改数据")
                                           Employee employee){
        try {
            if (employee.getId()==null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }

    }
    //@RequestMapping("/delete")
    @DeleteMapping("/{id}")
    @RonghuanetPermission(name = "删除员工")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }

    }

    //@RequestMapping("/queryById")

    @GetMapping("/{id}")
    @RonghuanetPermission(name = "员工查询单条")
    public Employee queryById(@PathVariable("id") Long id){
        try {
            return employeeService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //@RequestMapping("/queryAll")
    @GetMapping
    @RonghuanetPermission(name = "员工查询全部")
    public List<Employee> queryAll(){
        try {
            return employeeService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @PostMapping
    @RonghuanetPermission(name = "员工的分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        try {
            return employeeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     方案1:
        for(Long id :ids){
            employeeService.deleteById(id)
        }

     方案2:
       ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        select *  from t_org_employee where id in(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

        delete from t_org_employee where id in(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

        动态sql foreach



     */
    @PatchMapping
    @RonghuanetPermission(name = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {

            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }

    }
}
